<?php
namespace app\Index\model;

use think\Db;
use think\Model;
use app\index\model\Consult;

class User extends Model
{
	//查询用户
	public function get_MemberOne($where)
	{
		if (empty($where)) {
            return false;
        }
        return DB::name('users')->where($where)->find();
	}

	/**
     * 添加会员
     * @access public
     * @param member_info 用户信息
     */
	public function getMemberInsert($member_info)
	{
		if (empty($member_info)) {
            return false;
        }
        return Db::name('users')->insert($member_info);
	}

	/**
     * 添加关联表
     * @access public
     * @param user_id 用户id
     */
	public function getMemberGatherAdd($user_id)
	{
		return Db::name('users_gather')->insert(array('user_id'=>$user_id));
	}

	/**
     * 获取单个用户信息
     * @access public
     * @param where 条件
     */
	public function getMemberInfo($where,$field='*')
	{
		$member = Db::name('users')->field($field)->where(array('user_id'=>$where))->find();
		$rank = Db::name('rank')->where(array('rank_id'=>$member['rank_id']))->find();
		$member['list'] = Db::name('consult')->field('title,id')->where(array('user_id'=>$member['user_id'],'is_show'=>1))->limit(3)->select();
		$member['rank_name'] = $rank['rank_name'];
		return $member;
	}

	/**
     * 生成随机token
     * @access public
     * @param user_id 用户id
     */
	public function getToken($user_id)
	{
		$token = $this->str_rand($length=32);
		$result = Db::table('tp_users')->where(array('user_id'=>$user_id))->update(array('token'=>$token));
		if ($result) {
			return $token;
		}else{
			return false;
		}
	}

	//随机查询3个作者
	public function get_Gather()
	{
		$user = Db::name('users')->where('user_id >= ( SELECT floor(RAND() * (SELECT MAX(user_id) FROM tp_users)) )')->limit(3)->select();
		foreach ($user as $key => $value) {
			$user[$key]['consult_list'] = Db::name('consult')->where(array('user_id'=>$value['user_id'],'is_show'=>1))->limit(3)->select();
		}
		return $user;
	}

	//收藏
	public function getCollect($id,$userId)
	{
		$gather = Db::name('users_gather')->field('collect')->where(array('user_id'=>$userId))->find();
		if ($gather['collect']) {
			$collect = explode(',',$gather['collect']);
			//记录文章是否收藏
			$is_cz = in_array($id,$collect);
			if ($is_cz) {
				$result = [
					'msg' => '你已收藏了',
					'result' => 2
				];
				return $result;
			}else{
				$data['collect'] = $gather['collect'].','.$id.',';
				$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
				$result = [
					'msg' => '收藏成功',
					'result' => $res
				];
				return $result;
			}
		}else{
			$data['collect'] = ','.$id.',';
			$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
			$result = [
				'msg' => '收藏成功',
				'result' => $res
			];
			return $result;
		}
	}

	//关注接口
	//userIds 被关注的人
	//userId 关注的人
	public function getAttention($userIds,$userId)
	{
		$gather = Db::name('users_gather')->field('attention')->where(array('user_id'=>$userId))->find();
		$gathers = Db::name('users_gather')->field('fans')->where(array('user_id'=>$userIds))->find();
		//查看是否有关注的人
		if ($gather['attention']) {
			$attention = explode(',',$gather['attention']);
			//记录文章是否关注
			$is_cz = in_array($userIds,$attention);
			if ($is_cz) {
				$result = [
					'msg' => '你已关注了',
					'result' => 2
				];
				return $result;
			}else{
				$data['attention'] = $gather['attention'].','.$userIds.',';
				$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
				$result = [
					'msg' => '关注成功',
					'result' => $res
				];
				return $result;
			}
		}else{
			//关注
			$data['attention'] = ','.$userIds.',';
			$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
			//给我关注的人加粉
			//查看他是后有粉丝
			if ($gathers['fans']) {
				$fans = explode(',',$gathers['fans']);
				$is_fs = in_array($userIds,$attention);
				if (!$is_fs) {
					$data['fans'] = $gathers['fans'].','.$userIds.',';
					Db::name('users_gather')->where(array('user_id'=>$userIds))->update($data);
				}
			}else{
				$data['fans'] = ','.$userId.',';
				Db::name('users_gather')->where(array('user_id'=>$userIds))->update($data);
			}
			$result = [
				'msg' => '关注成功',
				'result' => $res
			];
			return $result;
		}
	}

	public function getAuthorInfo($userId)
	{
		$author = Db::name('users')->field('member_name,signature,consult,browse,like,diss,head_pic')->where(array('user_id'=>$userId))->find();
		$author['consult_list'] = $this->getUserConsult($userId);
		$author['comments_list'] = $this->getUserComments($userId);
		return $author;
	}

	public function getUserConsult($userId)
	{
		$new = new Consult();
		$consult = Db::table('tp_consult a')
		->field('a.id,a.title,a.img,a.user_id,a.member_name,a.abstract,a.addtime,a.look,a.like,a.click,a.comments,a.label')
		->field('b.member_name,b.head_pic')
		->join('tp_users b','a.user_id = b.user_id')
		->where(array('a.user_id'=>$userId,'a.is_show'=>1))->select();
		foreach ($consult as $key => $value) {
			$label = explode(",",$value['label']);
        	$i=0;
	        foreach ($label as $k => $v) {
	            if ($v !='') {
	                $label_v[$i] = $new->getLabelOne($v);
	                $i++;
	            }
	        }
	        $consult[$key]['label_v'] = $label_v;
		}
		return $consult;
	}

	public function getUserComments($userId)
	{
		return Db::name('comments')->where(array('user_id'=>$userId))->select();
	}

	public function getAuthor($order)
	{
		if ($order == 3) {
			$ord = 'liveness desc';
		}elseif($order == 2){
			$ord = 'currency desc';
		}else{
			$ord = 'major desc';
		}
		return Db::name('users')->where(array('is_author'=>1))->order($ord)->select();
	}

	/**
     * 生成随机字符串
     * @access public
     * @param length 字符串长度
     */
	public function str_rand($length = 6, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
		if(!is_int($length) || $length < 0) {
			return false;
		}
		$string = '';
		for($i = $length; $i > 0; $i--) {
			$string .= $char[mt_rand(0, strlen($char) - 1)];
		}
		return $string;
	}
}

